import {
  queryIndicators,
  queryAnalysis,
  queryStatistics,
  queryScadeStation,
  queryMonitorMetas,
} from '../services/index';

export default {
    namespace: 'production',

    state: {
      SourceResource0:[],
      SourceResource1:[],
      SourceResource2:[],
      SupplyResource:[],
      DrugResource:[],
      ElectricResource:[],
      AnalysisResource: {},
      IndicatorsResource: {},
      StatisticsResource: {},
      StatisticsResource1: [
        { time: '2019-01', val: 20 },
        { time: '2019-02', val: 24 },
        { time: '2019-03', val: 21 },
        { time: '2019-04', val: 22 },
        { time: '2019-05', val: 23 },
        { time: '2019-06', val: 20 },
        { time: '2019-07', val: 28 },
        { time: '2019-08', val: 0 },
        { time: '2019-09', val: 0 },
        { time: '2019-10', val: 0 },
        { time: '2019-11', val: 0 },
        { time: '2019-12', val: 0 },
      ],
      AnalysisResource5: [
        { time: '2019-01', val: 20 },
        { time: '2019-02', val: 24 },
        { time: '2019-03', val: 21 },
        { time: '2019-04', val: 22 },
        { time: '2019-05', val: 23 },
        { time: '2019-06', val: 20 },
        { time: '2019-07', val: 28 },
        { time: '2019-08', val: 0 },
        { time: '2019-09', val: 0 },
        { time: '2019-10', val: 0 },
        { time: '2019-11', val: 0 },
        { time: '2019-12', val: 0 },
      ],
      AnalysisResource3: [
        { time: '2019-01', val: 6 },
        { time: '2019-02', val: 4 },
        { time: '2019-03', val: 7 },
        { time: '2019-04', val: 22 },
        { time: '2019-05', val: 3 },
        { time: '2019-06', val: 10 },
        { time: '2019-07', val: 8 },
        { time: '2019-08', val: 0 },
        { time: '2019-09', val: 0 },
        { time: '2019-10', val: 0 },
        { time: '2019-11', val: 0 },
        { time: '2019-12', val: 0 },
      ],
      AnalysisResource6: [
        { time: '2019-01', val: 20 },
        { time: '2019-02', val: 24 },
        { time: '2019-03', val: 21 },
        { time: '2019-04', val: 22 },
        { time: '2019-05', val: 23 },
        { time: '2019-06', val: 20 },
        { time: '2019-07', val: 28 },
        { time: '2019-08', val: 0 },
        { time: '2019-09', val: 0 },
        { time: '2019-10', val: 0 },
        { time: '2019-11', val: 0 },
        { time: '2019-12', val: 0 },
      ]
    },

    effects: {
      *querySource({ payload }, { call, put }) {
        // 解构 type，返回结果拼接
        const { type } = payload;
        const data = yield call(queryScadeStation, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`SourceResource${type}`]: data.Response.Sensors,
            },
          })
        } else {
          throw data
        }
      },
      *querySupply({ payload }, { call, put }) {
        // 解构 type，返回结果拼接
        const { type } = payload;
        const data = yield call(queryScadeStation, payload)
        const { success, message, status, ...other } = data
        let Sensors = [];
        if(data.Response.Sensors){
          data.Response.Sensors.map(term => {
              const { Id } = term;
              if(Id && "HD76_103_P1" == Id){
                Sensors.push(term);
              }
            }
          )
        }
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`SupplyResource${type}`]: Sensors,
            },
          })
        } else {
          throw data
        }
      },
      *queryDrug({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryMonitorMetas, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`DrugResource${type}`]: other.features,
            },
          })
        } else {
          throw data
        }
      },
      *queryElectric({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryMonitorMetas, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`ElectricResource${type}`]: other.features,
            },
          })
        } else {
          throw data
        }
      },
      *queryIndicators({ payload }, { call, put }) {
        const data = yield call(queryIndicators, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              IndicatorsResource: other,
            },
          })
        } else {
          throw data
        }
      },
      *queryAnalysis({ payload }, { call, put }) {
        // 解构 type，返回结果拼接
        const { type } = payload;
        const data = yield call(queryAnalysis, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`AnalysisResource${type}`]: other.features,
            },
          })
        } else {
          throw data
        }
      },
      *queryStatistics({ payload }, { call, put }) {
        const { type } = payload;
        const data = yield call(queryStatistics, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`StatisticsResource${type}`]: other.data,
            },
          })
        } else {
          throw data
        }
      },
    },

    reducers: {
      setState(state, action) {
        return {
            ...state,
            ...action.payload
        };
      }
    },
  }
